package com.fenda.jetpacknavigation.roomdata;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;

import java.util.List;

import io.reactivex.Completable;
import io.reactivex.Flowable;

public class MyUserViewModel extends AndroidViewModel implements LifecycleObserver {

    private UserDao userDao;
    //ViewModel用来管理数据类  Activity用来控制
    public MyUserViewModel(@NonNull  Application application) {
        super(application);
        UserDatabase userDatabase = UserDatabase.getInstance(application);
        userDao = userDatabase.getUserDao();
    }

    Flowable<List<User>> getUsers(){
       return userDao.getAllNames();
    }

    public Completable insert(User user){
        return userDao.insertName(user);
    }

    Flowable<User> getUserById(int id){
        return userDao.getUser(id);
    }

    Completable deleteAll(){
        return userDao.deleteAll();
    }

    Completable updateUser(User...user){
        return userDao.updateName(user);
    }

}
